package com.sun.util;

import org.commonmark.Extension;
import org.commonmark.ext.gfm.tables.TableBlock;
import org.commonmark.ext.gfm.tables.TablesExtension;
import org.commonmark.ext.heading.anchor.HeadingAnchorExtension;
import org.commonmark.node.Link;
import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.AttributeProvider;
import org.commonmark.renderer.html.AttributeProviderContext;
import org.commonmark.renderer.html.AttributeProviderFactory;
import org.commonmark.renderer.html.HtmlRenderer;

import java.util.*;

public class MarkdownUtils {

    /**
     * markdown格式转换成HTML格式
     * @param markdown
     * @return
     */
    public static String markdownToHtml(String markdown) {
        Parser parser = Parser.builder().build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder().build();
        return renderer.render(document);
    }

    /**
     * 增加扩展[标题锚点，表格生成]
     * Markdown转换成HTML
     * @param markdown
     * @return
     */
    public static String markdownToHtmlExtensions(String markdown) {
        //h标题生成id
        Set<Extension> headingAnchorExtensions = Collections.singleton(HeadingAnchorExtension.create());
        //转换table的HTML
        List<Extension> tableExtension = Arrays.asList(TablesExtension.create());
        Parser parser = Parser.builder()
                .extensions(tableExtension)
                .build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder()
                .extensions(headingAnchorExtensions)
                .extensions(tableExtension)
                .attributeProviderFactory(new AttributeProviderFactory() {
                    public AttributeProvider create(AttributeProviderContext context) {
                        return new CustomAttributeProvider();
                    }
                })
                .build();
        return renderer.render(document);
    }

    /**
     * 处理标签的属性
     */
    static class CustomAttributeProvider implements AttributeProvider {
        @Override
        public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
            //改变a标签的target属性为_blank
            if (node instanceof Link) {
                attributes.put("target", "_blank");
            }
            if (node instanceof TableBlock) {
                attributes.put("class", "ui celled table");
            }
        }
    }


    public static void main(String[] args) {
        String table = "| hello | hi   | 哈哈哈   |\n" +
                "| ----- | ---- | ----- |\n" +
                "| 斯维尔多  | 士大夫  | f啊    |\n" +
                "| 阿什顿发  | 非固定杆 | 撒阿什顿发 |\n" +
                "\n";
        String a = "**参考b站的codesheep的视频**\n" +
                "*win10版*\n" +
                "\n" +
                "<!-- more -->\n" +
                "## 1.安装Node.js\n" +
                "安装官网: [点这里](https://nodejs.org)\n" +
                "安装步骤根据默认即可\n" +
                "\n" +
                "\n" +
                "## 2.安装git\n" +
                "安装官网: [node.js](https://git-scm.com/downloads)\n" +
                "如果安装的很缓慢的话可以进入淘宝镜像网站：[点这里](https://npm.taobao.org/mirrors/git-for-windows/)\n" +
                "安装步骤也是默认\n" +
                "我下载的版本：Git-2.21.0.rc0.windows.1-64-bit.exe\n" +
                "安装完成后可以在开始菜单看到\n" +
                "![](https://rong-1257752702.cos.ap-chengdu.myqcloud.com/hexo/hexo%E6%95%99%E7%A8%8B/2.png)\n" +
                "\n" +
                "## 3.安装Hexo\n" +
                "### (一).创建文件夹\n" +
                "在你的电脑的任意一个盘下创建一个文件夹，这个文件夹是用来储存博客的本地文件的，比如我就在f盘建立了一个blog文件夹，如下：\n" +
                "![](https://rong-1257752702.cos.ap-chengdu.myqcloud.com/hexo/hexo%E6%95%99%E7%A8%8B/3.png)\n" +
                "\n" +
                "### (二)使用Git Bash\n" +
                "在这个空文件夹中右键打开**Git Bash Here**进入如下的界面![](https://rong-1257752702.cos.ap-chengdu.myqcloud.com/hexo/hexo%E6%95%99%E7%A8%8B/4.png)\n" +
                "\n" +
                "输入node -v 和 npm -v 看是否出现版本，检测是否安装顺利\n" +
                "\n" +
                "![](https://rong-1257752702.cos.ap-chengdu.myqcloud.com/hexo/hexo%E6%95%99%E7%A8%8B/5.png)\n" +
                "\n" +
                "### (三)安装cnpm\n" +
                "输入如下的代码：\n" +
                "`npm install -g cnpm --registry=https://registry.npm.taobao.org`\n" +
                "\n" +
                "这玩意主要下载东西快\n" +
                "\n" +
                "安装完输入**cnpm**检测是否安装成功,出现以下内容即为成功\n" +
                "![](https://rong-1257752702.cos.ap-chengdu.myqcloud.com/hexo/hexo%E6%95%99%E7%A8%8B/6.png)\n" +
                "\n" +
                "### (四)安装hexo\n" +
                "\n" +
                "输入代码如下：\n" +
                "`cnpm install -g hexo-cli`\n" +
                "然后再输入`hexo -v`检测是否安装成功\n" +
                "安装成功界面：\n" +
                "![](https://rong-1257752702.cos.ap-chengdu.myqcloud.com/hexo/hexo%E6%95%99%E7%A8%8B/7.png)\n" +
                "\n" +
                "接下来输入`hexo init`初始化这个项目\n" +
                "\n" +
                "![](https://rong-1257752702.cos.ap-chengdu.myqcloud.com/hexo/hexo%E6%95%99%E7%A8%8B/8.png)\n" +
                "\n" +
                "### (五)运行测试\n" +
                "这里有三个代码是进行运行测试的关键\n" +
                "```\n" +
                "hexo g\n" +
                "hexo s\n" +
                "hexo d\n" +
                "```\n" +
                "输入`hexo g`\n" +
                "\n" +
                "![](https://rong-1257752702.cos.ap-chengdu.myqcloud.com/hexo/hexo%E6%95%99%E7%A8%8B/9.png)\n" +
                "\n" +
                "输入`hexo s`\n" +
                "\n" +
                "![](https://rong-1257752702.cos.ap-chengdu.myqcloud.com/hexo/hexo%E6%95%99%E7%A8%8B/10.png)\n" +
                "\n" +
                "这时可以进入`http://localhost:4000`查看博客本地化了\n" +
                "\n" +
                "预览图(这是我修改过主题的，官方初始预览和我这不一样)\n" +
                "\n" +
                "![](https://rong-1257752702.cos.ap-chengdu.myqcloud.com/hexo/hexo%E6%95%99%E7%A8%8B/11.png)\n" +
                "\n" +
                "## 本地内容搭建完成\n" +
                "## 接下来的步骤在下一篇";
        System.out.println(markdownToHtmlExtensions(a));
    }
}
